// pages/movies/detail/detail.js
import { httpMore } from '../../../utils/util'

let app = getApp();
const globalData = app.globalData;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    subject_collection_items: [],
    pageSize: 0,//存放已经加载的数据个数
    page: 0     //存放加载次数
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const id = options.id
    this.setData({
      id: id
    })
    const moreUrl = `${globalData.g_requestApi}subject_collection/${id}/items?start=0&count=20&apiKey=054022eaeae0b00e0fc068c0c0a2102a`
    console.log(id)
    httpMore(moreUrl, this.processData)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  //跳转到详情页面
  onTapDetail: function (event) {
    console.log(event)
    const id = event.currentTarget.dataset.id
    const type = event.currentTarget.dataset.type
    wx.navigateTo({
      url: `../detail/detail?id=${id}&&type=${type}`
    })
  },
  //滑到底部请求更多数据
  loadMore: function () {
    let moreUrl = `${globalData.g_requestApi}subject_collection/${this.data.id}/items?start=${this.data.subject_collection_items.length}&count=20&apiKey=054022eaeae0b00e0fc068c0c0a2102a`
    console.log('加载更多')
    httpMore(moreUrl, this.processData)
  },
  
  //回调赋值
  processData: function (data) {
    let page = this.data.page + 1
    let pageSize = this.data.pageSize + 20
    if (this.data.page === 0) {
      this.setData({
        subject_collection_items: data.subject_collection_items,
        total: data.total,
        page: page,
        pageSize: pageSize
      })
    } else if (this.data.pageSize < this.data.total) {
      let subject_collection_items = this.data.subject_collection_items.concat(data.subject_collection_items)
      this.setData({
        subject_collection_items: subject_collection_items,
        page: page,
        pageSize: pageSize
      })
    } else {
      wx.showToast({
        title: '没有更多数据'
      })
    }
  }
})